package com.example.combwyaq.mvp.model;

import com.example.combwyaq.entity.ResponseUserEntity;
import com.example.combwyaq.http.Api;
import com.example.combwyaq.http.HttpFactory;
import com.example.combwyaq.http.HttpType;
import com.example.combwyaq.mvp.ChangeFuncation;
import com.example.combwyaq.mvp.contract.UserDetailsContract;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.visualinterface.user.entity.UpdateUserEntity;
import com.example.combwyaq.visualinterface.user.entity.UploadFileEntity;

import java.io.File;
import java.util.Map;
import javax.inject.Inject;
import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserDetailsModel extends BaseModel implements UserDetailsContract.IUserDetailsModel {

@Inject
public UserDetailsModel(){}

@Override
public Observable<BaseEntity> requestUser(Map<String, Object> map) {
        return HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE).getRetrofit()
        .create(Api.class).requestUser(createBody(map)).map(new ChangeFuncation<ResponseUserEntity>());
        }

    @Override
    public Observable<BaseEntity> updateUser(Map<String, Object> map) {
        return null;
    }

/*
@Override
public Observable updateUser(Map<String, Object> map) {
        return  HttpFactory
                .getInstance()
                .factory(HttpType.TOKENSIGNTYPE)
                .getRetrofit()
                .create(Api.class)
                .requestUpdateUser(createBody(map))
                .map(new ChangeFuncation<UpdateUserEntity>());
        }
*/

@Override
public Observable<UploadFileEntity> uploadHead(Map<String, Object> map) {
        String str = (String) map.get("file");
        File file = new File(str);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("file",str.substring(str.lastIndexOf("/")+1),requestBody);
        return HttpFactory.getInstance().factory(HttpType.UPLOADTYPE).getRetrofit().create(Api.class)
        .requestUploadHead(builder.build().parts());
        }
}
